package com.gitee.wsl.jvm.io.resources

import java.io.IOException
import java.net.URI
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.ProviderNotFoundException
import kotlin.Any
import kotlin.String
import kotlin.Throws
import kotlin.require

class ResourcesFileSystemProvider : FileSystemProvider {
    private var fileSystem: FileSystem? = null

    @Throws(IOException::class)
    override fun toPath(uri: URI): Path {
        try {
            require("resource" == uri.scheme) { "ResourcesFileSystemProvider only supports uri's starting with resource:" }

            val fs = getFileSystem()
            return fs!!.getPath(uri.toString().substringAfter(":"))
        } catch (e: ProviderNotFoundException) {
            throw ProviderNotFoundException("Provider not found for URI $uri")
        }
    }

    @Throws(IOException::class)
    private fun getFileSystem(): FileSystem? {
        if (fileSystem == null) {
            val options: MutableMap<String, Any?> = HashMap()
            options["create"] = true
            fileSystem = FileSystems.newFileSystem(URI.create("resource:/resources"), options, null)
        }
        return fileSystem
    }

    @Throws(IOException::class)
    override fun close() {
        fileSystem!!.close()
    }
}
